<template>
	<view>
		<view class="box">
			<view class="upload">
				<image :src="imgOcrT" @click="realname('front')" mode="widthFix"></image>
				<image :src="imgOcrF" @click="realname('back')" mode="widthFix" style="margin-top: 20rpx;"></image>
			</view>
			<view class="ul">
				<view class="li">
					<view class="left">姓名<view class="item-xing">*</view>
					</view>
					<input type="text" v-model="userName" placeholder="请输入真实姓名" />
				</view>
				<view class="li">
					<view class="left">身份证号<view class="item-xing">*</view>
					</view>
					<input type="text" v-model="userNumber" placeholder="请输入身份证号码" />
				</view>
				<view class="li">
					<view class="left">预留手机号<view class="item-xing">*</view>
					</view>
					<input type="text" v-model="phoneNumber" placeholder="请输入预留手机号" style="padding-right: 200rpx;"/>
					<button class="btn" size="mini" @tap="countDownPassCode" :disabled="disableGetMobileCode">{{getCodeButtonText}}</button>
				</view>
				<view class="li">
					<view class="left">验证码<view class="item-xing">*</view>
					</view>
					<input type="text" v-model="smsCode" placeholder="请输入验证码" />
				</view>
			</view>
			<!-- <view class="enroll-item">
				<view class="item-title">收款人姓名</view>
				<view class="item-xing">*</view>
				<input class="item-content" type="text" placeholder="请输入姓名" v-model="name"></input>
			</view> -->
			<view class="enroll-item" style="margin-top: 2rpx;">
				<view class="item-title">银行卡号<view class="item-xing">*</view></view>
				<input class="item-content" type="number" placeholder="请输入银行卡号" v-model="cardNumber"
					@blur="getUserIdCardNumber"></input>
			</view>
			<view class='enroll-item select_box' style="margin-top: 2rpx;">
				<view class="item-title">卡类型<view class="item-xing">*</view></view>
				<view class='item-content select' @tap='cardTypeSelectTap'>
					<text class='select_text'>{{cardType}}</text>
				</view>
				<view class='mask' v-if="cardTypeShow" @tap='cardTypeSelectTap'></view>
			</view>
			<view class="bank-reminder">微信提现仅支持可选银行列表</view>

			<view class='enroll-item select_box' style="margin-top: 2rpx;">
				<view class="item-title">银行名称<view class="item-xing">*</view></view>
				<view class='item-content select' @tap='bankTypeSelectTap'>
					<text class='select_text'>{{bankName}}</text>
				</view>
				<view class='mask' v-if="bankTypeShow" @tap='bankTypeSelectTap'></view>
			</view>
			<view class="withdraw-note">提现须知</view>
			<view class="bank-reminder"></view>
			<view class="bank-reminder">
				请在上方填写您的真实的个人信息并与身份证信息一致，自动填充的信息请勿更改，信息出错或者不一致会导致您提现失效。我们承诺您的资料只会在本平台用户提现打款时使用不会对外使用，请放心填写。</view>
		</view>
		<!-- 银行卡类型 -->
		<view class='option_box' :style="'height:'+(cardTypeShow?(cardTypeList.length*70+10):0)+'rpx;'">
			<text class='option' :style='index==cardTypeList.length-1&&"border:0;"'
				v-for='(item, index) in cardTypeList' :key='index' @tap='cardTypeOptionTap(index)'>{{item}}</text>
		</view>
		<!-- 选择银行名称 -->
		<view class='option_box'
			:style="'height:'+(bankTypeShow?(bankTypeList.length>10?800:bankTypeList.length*80):0)+'rpx;'">
			<text class='option' :style='index==bankTypeList.length-1&&"border:0;"'
				v-for='(item, index) in bankTypeList' :key='item.id'
				@tap='bankTypeOptionTap(index)'>{{item.bankName}}</text>
		</view>

		<!-- <view class="enroll-item" style="margin-top: 2rpx;">
		  <view class="item-title">银行名称</view>
		  <view class="item-xing">*</view>
		  <input class="item-content" type="text" placeholder="请输入支行名称" v-model="bankName"></input>
		</view> -->

		<view class="sign-up" @tap="submitInfos">提交</view>
	</view>
</template>

<script>
	const util = require("@/utils/util.js")
	const bindingUtil = require('@/utils/bindingUtil.js');
	const ocrfunc = require('@/utils/ocrfunc.js');
	const client_id = 'Oce2cBtaNHkWgloNTzYk13if';
	const client_secret = 'XatLxGRgVOXzAQv7uVvzMOWU4pHPcuEU';
	export default {
		data() {
			return {
				name: '',
				cardNumber: '',
				cardType: '请选择卡类型',
				bankName: '请选择银行',
				bankId: '',
				bankTypeList: [],
				cardTypeList: [
					"储蓄卡",
					"信用卡",
					"准贷记卡",
					"预付费卡"
				],
				cardTypeShow: false,
				bankTypeShow: false,

				access_token: null, // 实名百度的token
				userName: "", // 实名名称
				userNumber: "", // 实名号码
				imgUpdateT: "", // 正面身份证数据
				imgUpdateF: "", // 背面身份证数据
				imgOcrT: '../../../static/images/ocrT.png', //身份证识别正面
				imgOcrF: '../../../static/images/ocrF.png', //身份证识别正面
				phoneNumber: '',
				smsCode: '',
				isSend: false,
				disableGetMobileCode: false,
				disableSubmitMobileCode: true,
				getCodeButtonText: '获取验证码',
			}
		},
		onLoad: function() {
			this.getBankTypeList();
			this.getAccess_token();
		},
		methods: {
			countDownPassCode: function() {
				let that = this;
				if (!util.isMobile(that.phoneNumber)) {
					return
				}
				uni.showLoading({
					title: '发送中...'
				})
				util.request('index/smsCode', {
						phone: that.phoneNumber
					}, 'POST', 'application/json')
					.then(function(res) {
						if (res.code == 0) {
							that.isSend = true
							util.toast(res.msg)
							var i = 60;
							var intervalId = setInterval(function() {
								i--
								if (i <= 0) {
									that.disableGetMobileCode = false
									that.disableSubmitMobileCode = false
									that.getCodeButtonText = '获取验证码'
									clearInterval(intervalId)
								} else {
									that.getCodeButtonText = i
									that.disableGetMobileCode = true
									that.disableSubmitMobileCode = false
								}
							}, 1000);
						} else {
							util.toast('发送失败');
						}
					});
			},
			getAccess_token() {
				let that = this;
				uni.request({
					url: 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' +
						client_id + '&client_secret=' + client_secret,
					method: 'POST',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					data: {},
					success(_res) {
						uni.hideLoading();
						that.access_token = _res.data.access_token;
					},
					fail(_res) {
						uni.hideLoading();
						uni.showToast({
							title: '请求出错',
						})
						reject(_res)
					}
				})
			},
			realname(frontOrBack) {
				ocrfunc.OcrIdCard(this.access_token, frontOrBack).then(res => {
					let userInfo = res.info.data
					if (frontOrBack == "front") {
						if (userInfo.image_status == 'normal') {
							this.imgOcrT = res.img
							this.imgUpdateT = res.img
							this.userName = userInfo.words_result['姓名'].words;
							this.userNumber = userInfo.words_result['公民身份号码'].words;
						} else {
							uni.showToast({
								icon: 'none',
								title: '请上传正确的正面身份证'
							});
							this.imgOcrT = "../../../static/images/ocrT.png";
							this.imgUpdateT = "";
							this.userName = "";
							this.userNumber = "";
						}
					} else {
						if (userInfo.image_status == 'normal') {
							this.imgOcrF = res.img
							this.imgUpdateF = res.img
						} else {
							uni.showToast({
								icon: 'none',
								title: '请上传正确的反面身份证'
							});
							this.imgOcrF = "../../../static/images/ocrF.png";
							this.imgUpdateF = "";
						}
					}
				}).catch(err => {
					console.log(err);
				})
			},
			//银行卡号
			getUserIdCardNumber: function(e) {
				let that = this;
				if (e.detail.value == null || e.detail.value == '') {
					return
				}
				that.cardNumber = e.detail.value

				var temp = bindingUtil.bankCardAttribution(e.detail.value)
				console.log(temp)
				if (temp == Error || temp == 'error') {
					that.cardType = '请选择卡类型'
					that.bankName = '请选择银行'
					that.bankId = ''
					util.toast('查找不到相关银行信息')
				} else {
					that.cardType = temp.cardTypeName
					for (let index in that.bankTypeList) {
						let bankType = that.bankTypeList[index]
						if (bankType.bankName == temp.bankName) {
							that.bankName = bankType.bankName
							that.bankId = bankType.id
							return
						}
					};
					that.bankName = '请选择银行'
					that.bankId = ''

					util.toast('银行名称查找不到')
				}
			},
			//提交转账信息
			submitInfos: function() {
				var compare = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
				var that = this;
				if (!that.imgUpdateT) {
					util.toast('请上传身份证正面照')
				} else if (!that.imgUpdateF) {
					util.toast('请上传身份证反面照')
				} else if (!that.userName) {
					util.toast('身份证姓名不能为空')
				} else if (!that.userNumber) {
					util.toast('身份证号不能为空')
				} else if (!that.phoneNumber) {
					util.toast('手机号不能为空')
				} else if (!that.smsCode) {
					util.toast('验证码不能为空')
				} else if (!that.cardNumber) {
					util.toast('银行卡号不能为空')
				} else if (!that.cardType || that.cardType == '请选择卡类型') {
					util.toast('卡类型不能为空')
				} else if (!that.bankName || that.bankName == '请选择银行') {
					util.toast('支行名称不能为空')
				} else {
					//TODO post data to sever
					util.request('myCard/bindBank', {
						cardName: that.userName,
						idCard: that.userNumber,
						idNumberFrontUrl: that.imgUpdateT,
						idNumberBackUrl: that.imgUpdateF,
						bankCard: that.cardNumber,
						cardType: that.cardType,
						bankTypeId: that.bankId,
						phoneNumber: that.phoneNumber,
						smsCode: that.smsCode
					}, 'POST').then(function(res) {
						if (res.code === 0) {
							util.toast('绑定成功')
							setTimeout(function() {
								uni.navigateBack({
									delta: 1,
								})
							}, 1000)
						} else {
							util.toast(res.msg);
						}
					});
				}
			},
			getBankTypeList: function() {
				let that = this
				util.request('user/bankTypeList').then(res => {
					if (res.code === 0) {
						that.bankTypeList = res.data;
					}
				});
			},
			// 点击下拉显示框
			cardTypeSelectTap() {
				this.cardTypeShow = !this.cardTypeShow
			},
			// 点击下拉列表
			cardTypeOptionTap(Index) {
				this.cardTypeShow = !this.cardTypeShow
				this.cardType = this.cardTypeList[Index]
			},
			closeCardTypeSelect() {
				this.cardTypeShow = !this.cardTypeShow
			},
			bankTypeSelectTap() {
				this.bankTypeShow = !this.bankTypeShow
			},
			bankTypeOptionTap(Index) {
				this.bankTypeShow = !this.bankTypeShow
				this.bankName = this.bankTypeList[Index].bankName
				this.bankId = this.bankTypeList[Index].id
			}
		}
	}
</script>

<style>
	page {
		background-color: #eaeaea;
	}

	.box {
		width: 100%;
		padding: 30rpx 20rpx;
	}

	.upload {
		width: 100%;
		background: #fff;
		text-align: center;
		padding: 30rpx 0;
		border-radius: 20rpx;
	}

	.upload image {
		width: 584rpx;
		height: auto;
	}

	.ul {
		width: 100%;
		background: #fff;
		padding: 0 24rpx;
		margin-top: 40rpx;
	}

	.li {
		width: 100%;
		border-bottom: 1rpx solid #f5f5f5;
		font-size: 32rpx;
		color: #343434;
		font-weight: bold;
		display: flex;
		align-items: center;
		height: 80rpx;
		position: relative;
	}

	.li input {
		text-align: left;
		width: 100%;
		font-weight: initial;
		font-size: 32rpx;
	}

	.li .btn {
		position: absolute;
		right: 0;
		top: 20rpx;
		width: 160rpx;
	}

	.li .left {
		width: 220rpx;
		flex-shrink: 0;
	}

	.container {
		padding: 0rpx;
	}

	.enroll-item {
		display: flex;
		flex-direction: row;
		background-color: white;
		border-radius: 0;
		width: 100%;
		height: 80rpx;
		flex-wrap: nowrap;
		align-items: center;
	}

	.item-title {
		font-size: 32rpx;
		color: #343434;
		white-space: nowrap;
		margin-left: 24rpx;
		font-weight: bold;
		width: 220rpx;
	}

	.item-xing {
		color: #fc6262;
		display: inline-block;
	}

	.item-content {
		font-size: 32rpx;
		color: #343434;
		flex: 1;
		text-align: left;
		padding-right: 24rpx;
	}

	.withdraw-note {
		font-size: 30rpx;
		margin-left: 20rpx;
		margin-top: 20rpx;
		margin-bottom: 20rpx;
	}

	.bank-reminder {
		font-size: 28rpx;
		margin-left: 20rpx;
		color: #8a8a8a;
		margin-top: 10rpx;
		margin-bottom: 10rpx;
	}

	.sign-up {
		font-size: 30rpx;
		height: 96rpx;
		display: flex;
		margin-top: 24rpx;
		justify-content: center;
		align-items: center;
		background-color: #3dc2ed;
		width: 100%;
		color: white;
	}

	.select_text {
		font-size: 30rpx;
		flex: 1;
	}

	.option_box {
		border-radius: 10rpx;
		-webkit-border-radius: 10rpx;
		position: fixed;
		bottom: 0;
		width: 100%;
		border: 1px solid #efefef;
		box-sizing: border-box;
		height: 0;
		overflow-y: auto;
		border-top: 0;
		background: #fff;
		transition: height 0.3s;
		z-index: 9999;

	}

	.option {
		display: block;
		line-height: 50rpx;
		font-size: 30rpx;
		color: #8a8a8a;
		border-bottom: 1px solid #efefef;
		padding: 10rpx;
		text-align: center;
	}

	.mask {
		width: 100%;
		height: 100%;
		position: fixed;
		top: 0;
		left: 0;
		background: #000;
		z-index: 9000;
		opacity: 0.7;
	}

	/* 隐藏滚动条 */
	::-webkit-scrollbar {
		width: 0;
		height: 0;
		color: transparent;
	}
</style>
